home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ip / ka9q / aztecnos.arc / PCGEN.ASM < prev    next >
Encoding:
Assembly Source File  |  1989-03-19  |  8.6 KB  |  438 lines

  1.     include lmacros.h
  2.  
  3.     assume    ds:dataseg
  4.     public Sssave,Spsave,Intstk_,doret,Isat_,dbase_,cbase_
  5.     extrn    _Dorg_:byte,_Corg_:byte
  6.  
  7.     ifdef    FARPROC
  8.     extrn    ctick_:far
  9.     else
  10.     extrn    ctick_:near
  11.     endif
  12.  
  13. dbase    dw    seg _Dorg_    ; save loc for ds (must be in code segment)
  14.  
  15. ; common routine for interrupt return
  16.  
  17.     ifdef    FARPROC
  18. doret    proc    far
  19.     else
  20. doret    proc    near
  21.     endif
  22.     cmp    Isat_,1
  23.     jnz    notat        ; Only one 8259, so skip this stuff
  24.     mov    al,0bh        ; read in-service register from
  25.     out    0a0h,al        ; secondary 8259
  26.     nop            ; settling delay
  27.     nop
  28.     nop
  29.     in    al,0a0h        ; get it
  30.     or    al,al        ; Any bits set?
  31.     jz    notat        ; nope, not a secondary interrupt
  32.     mov    al,20h        ; Get EOI instruction
  33.     out    0a0h,al        ; Secondary 8259 (PC/AT only)
  34. notat:    mov    al,20h        ; 8259 end-of-interrupt command
  35.     out    20h,al        ; Primary 8259
  36.      pop    es
  37.     pop    di
  38.     pop    si
  39.     pop    bp
  40.     pop    dx
  41.     pop    cx
  42.     pop    bx
  43.     pop    ax
  44.     mov    ss,Sssave
  45.     mov    sp,Spsave    ; restore original stack context
  46.     pop    ds
  47.     iret
  48. doret    endp
  49.  
  50. ; Null interrupt handler
  51.     procdef nullvec
  52.     iret
  53.     pend    nullvec
  54.  
  55. ; setvect - set interrupt vector
  56. ; called from C as follows
  57. ; setvect(vec,vecval)
  58. ; char vec;        /* Interrupt number */
  59. ; void (*vecval)();    /* offset (and segment in large code model) */
  60.  
  61.     procdef    setvect,<<vec,byte>,<ipval,fptr>>
  62.     mov    dx,word ptr ipval
  63.     push    ds        ; save
  64. ifdef    FARPROC
  65.     mov    ds,word ptr ipval+2
  66. else
  67.     mov    ax,cs
  68.     mov    ds,ax
  69. endif
  70.     mov    ah,25h
  71.     mov    al,vec
  72.     int    21h
  73.     pop    ds        ; restore
  74.     pret
  75.     pend    setvect
  76.  
  77. ; getvect - return current interrupt vector
  78. ; called from C as
  79. ; long        /* Returns CS in high word, IP in low word */
  80. ; getvect(vecnum)
  81. ; char vecnum;    /* Interrupt number */
  82.  
  83.     procdef    getvect,<<vecnum,byte>>
  84.     mov    ah,35h
  85.     mov    al,vecnum
  86.     push    es    ; save, since DOS uses it for a return value
  87.     int    21h
  88.     mov    dx,es    ; CS value into DX (C high word)
  89.     mov    ax,bx    ; IP value into AX (C low word)
  90.     pop    es    ; restore es
  91.     pret
  92.     pend    getvect
  93.  
  94. ; kbraw - raw, nonblocking read from console
  95. ; If character is ready, return it; if not, return -1
  96.  
  97.     procdef    kbraw
  98.     mov    ah,06h    ; Direct Console I/O
  99.     mov    dl,0ffh    ; Read from keyboard
  100.     int    21h    ; Call DOS
  101.     jz    nochar    ; zero flag set -> no character ready
  102.     mov    ah,0    ; valid char is 0-255
  103.     pret
  104. nochar:
  105.     mov    ax,-1    ; no char, return -1
  106.     pret
  107.     pend    kbraw
  108.  
  109. ; Return nonzero if a key is ready, without reading it
  110.     procdef    kbhit
  111.     mov    ah,0bh    ; Check Standard Input Status
  112.     int    21h    ; Call DOS
  113.     mov    ah,0    ; clear upper half of return value
  114.     pret
  115.     pend    kbhit
  116.  
  117. ; istate - return current interrupt state
  118.     procdef    istate
  119.     pushf
  120.     pop    ax
  121.     and    ax,200h
  122.     jnz    ion1
  123.     pret
  124. ion1:    mov    ax,1
  125.     pret
  126.     pend    istate
  127.  
  128. ; dirps - disable interrupts and return previous state: 0 = disabled,
  129. ;           1 = enabled
  130.  
  131.     procdef    dirps
  132.     pushf            ; save state on stack
  133.     cli            ; interrupts off
  134.     pop    ax        ; original flags -> ax
  135.     and    ax,200h        ; 1<<9 is IF bit
  136.     jnz    ion        ; nonzero -> interrupts were on
  137.     pret
  138. ion:    mov    ax,1
  139.     pret
  140.     pend    dirps
  141.  
  142. ; restore - restore interrupt state: 0 = off, nonzero = on
  143.  
  144.     procdef    restore,<<istate,byte>>
  145.     test    istate,0ffh
  146.     jz    ioff
  147.     sti
  148.     pret
  149. ioff:    cli
  150.     pret
  151.     pend    restore
  152.  
  153. ; enable - enable interrupts unconditionally
  154.     procdef    enable
  155.     sti
  156.     pret
  157.     pend    enable
  158.  
  159. ; Halt until an interrupt occurs, then return
  160.     procdef eihalt
  161.     sti    ; make sure interrupts are enabled
  162.     hlt
  163.     pret
  164.     pend    eihalt
  165.  
  166. ; multitasker types
  167. NONE        equ    0
  168. DOUBLEDOS    equ    1
  169. DESQVIEW    equ    2
  170.  
  171. ; Relinquish processor so other task can run
  172.     procdef    giveup
  173.     pushf        ;save caller's interrupt state
  174.     sti        ;re-enable interrupts
  175.     cmp    mtasker,DOUBLEDOS
  176.     jnz    givedesqview
  177.     mov    al,2        ; 110 ms
  178.     mov    ah,0eeh
  179.     int    21h
  180.     popf        ; restore interrupts
  181.     pret
  182. givedesqview:
  183.     cmp    mtasker,DESQVIEW
  184.     jnz    notask
  185.     mov    ax, 1000h
  186.     int    15h
  187.     popf        ; restore interrupts
  188.     pret
  189. notask:
  190.     hlt        ; wait for an interrupt
  191.     popf        ; restore interrupts
  192.     pret
  193.     pend    giveup
  194.  
  195. ; check for a multitasker running
  196.     procdef chktasker
  197.     mov    mtasker, NONE
  198.     ; do the doubledos test
  199.     mov    ah, 0e4h
  200.     int    21h
  201.     cmp    al, 1
  202.     jz    isdos
  203.     cmp    al, 2
  204.     jnz    test_desq
  205. isdos:    mov    mtasker, DOUBLEDOS
  206.     pret
  207.  
  208.     ; test for desqview
  209. test_desq:
  210.     mov    ax, 2b01h
  211.     mov    cx, 4445h
  212.     mov    dx, 5351h
  213.     int    21h
  214.     cmp    al, 0ffh
  215.     jnz    isdesq
  216.     pret
  217. isdesq:    mov    mtasker, DESQVIEW
  218.     pret
  219.     pend    chktasker
  220.  
  221. ; getss - Read SS for debugging purposes
  222.     procdef    getss
  223.     mov    ax,ss
  224.     pret
  225.     pend    getss
  226.  
  227. ; Internet checksum subroutine
  228. ; Compute 1's-complement sum of data buffer
  229. ; Uses an unwound loop inspired by "Duff's Device" for performance
  230. ;
  231. ; Called from C as
  232. ; unsigned short
  233. ; lcsum(buf,cnt)
  234. ; unsigned short *buf;
  235. ; unsigned short cnt;
  236.     procdef    lcsum,<<buf,ptr>,<cnt,word>>
  237.     pushds            ; save if using large model
  238.     push    si
  239.     ldptr    si,buf,ds    ; ds:si = buf
  240.     mov    cx,cnt        ; cx = cnt
  241.     cld            ; autoincrement si
  242.  
  243.     mov    ax,cx
  244.     shr    cx,1        ; cx /= 16, number of loop iterations
  245.     shr    cx,1
  246.     shr    cx,1
  247.     shr    cx,1
  248.  
  249.     inc    cx        ; make fencepost adjustment for 1st pass
  250.     and    ax,15        ; ax = number of words modulo 16
  251.     shl    ax,1        ; *=2 for word table index
  252.     lea    bx,jtable    ; bx -> branch table
  253.     add    bx,ax        ; index into jump table
  254.     clc            ; initialize carry = 0
  255.     mov    dx,0        ; clear accumulated sum
  256.     jmp    word ptr[bx]    ; jump into loop
  257.  
  258. ; Here the real work gets done. The numeric labels on the lodsw instructions
  259. ; are the targets for the indirect jump we just made.
  260. ;
  261. ; Each label corresponds to a possible remainder of (count / 16), while
  262. ; the number of times around the loop is determined by the quotient.
  263. ;
  264. ; The loop iteration count in cx has been incremented by one to adjust for
  265. ; the first pass.
  266. deloop:    lodsw
  267.     adc    dx,ax
  268. l15:    lodsw
  269.     adc    dx,ax
  270. l14:    lodsw
  271.     adc    dx,ax
  272. l13:    lodsw
  273.     adc    dx,ax
  274. l12:    lodsw
  275.     adc    dx,ax
  276. l11:    lodsw
  277.     adc    dx,ax
  278. l10:    lodsw
  279.     adc    dx,ax
  280. l9:    lodsw
  281.     adc    dx,ax
  282. l8:    lodsw
  283.     adc    dx,ax
  284. l7:    lodsw
  285.     adc    dx,ax
  286. l6:    lodsw
  287.     adc    dx,ax
  288. l5:    lodsw
  289.     adc    dx,ax
  290. l4:    lodsw
  291.     adc    dx,ax
  292. l3:    lodsw
  293.     adc    dx,ax
  294. l2:    lodsw
  295.     adc    dx,ax
  296. l1:    lodsw
  297.     adc    dx,ax
  298. l0:    loop    deloop        ; :-)
  299.  
  300.     adc    dx,0        ; get last carries
  301.     adc    dx,0
  302.     mov    ax,dx        ; result into ax
  303.     xchg    al,ah        ; byte swap result (8088 is little-endian)
  304.     pop    si
  305.     popds            ; all done
  306.     pret
  307.     pend    lcsum
  308.  
  309. ; Clock tick interrupt handler
  310.     public    btick_
  311. btick_    proc    far
  312.     ; Note that we don't save DS. It has already been saved by the BIOS,
  313.     ; and if we saved and restored it the indirect jmp at the end would
  314.     ; go off into the ozone.
  315.     mov    ds,cs:dbase    ; establish interrupt data segment
  316.  
  317.     mov    Sssave,ss    ; stash user stack context
  318.     mov    Spsave,sp
  319.  
  320.     mov    ss,cs:dbase
  321.     lea    sp,Intstk_+512
  322.  
  323.     push    ax        ; save user regs on interrupt stack
  324.     push    bx
  325.     push    cx
  326.     push    dx
  327.     push    bp
  328.     push    si
  329.     push    di
  330.     push    es
  331.     mov    ax,ds
  332.     mov    es,ax
  333.  
  334.     call    ctick_
  335.  
  336.      pop    es
  337.     pop    di
  338.     pop    si
  339.     pop    bp
  340.     pop    dx
  341.     pop    cx
  342.     pop    bx
  343.     pop    ax
  344.     mov    ss,Sssave
  345.     mov    sp,Spsave    ; restore original stack context
  346.     jmp    dword ptr Tvec_    ; link to previous vector
  347. btick_    endp
  348.  
  349. ; Convert 32-bit int in network order to host order (dh, dl, ah, al)
  350.     procdef    get32,<<cp32,ptr>>
  351.     pushds
  352.     cld
  353.     push    si
  354.     ldptr    si,cp32,ds
  355.     lodsw
  356.     mov    dh,al    ; high word to dx, a-swapping as we go
  357.     mov    dl,ah
  358.     lodsw
  359.     xchg    al,ah    ; low word stays in ax, just swap
  360.     pop    si
  361.     popds
  362.     pret
  363.     pend    get32
  364.  
  365. ; Convert 16-bit int in network order to host order (ah, al)
  366.     procdef    get16,<<cp16,ptr>>
  367.     pushds
  368.     cld
  369.     push    si
  370.     ldptr    si,cp16,ds
  371.     lodsw
  372.     xchg    al,ah    ; low word stays in ax, just swap
  373.     pop    si
  374.     popds
  375.     pret
  376.     pend    get16
  377.  
  378. ; Convert 32-bit int to network order, returning new pointer
  379.     procdef    put32,<<cpo32,ptr>,<pi32,dword>>
  380.     pushds
  381.     push    es
  382.     cld
  383.     push    di
  384.     push    si
  385.     ldptr    di,cpo32,es    ; point di to network output buffer
  386.     lea    si,pi32    ; point si to input doubleword
  387. ifdef    LONGPTR
  388.     mov    ax,ss
  389.     mov    ds,ax
  390. endif
  391.     lodsw        ; fetch low word of machine version
  392.     mov    dh,al    ; swap bytes and save
  393.     mov    dl,ah
  394.     lodsw        ; fetch high word
  395.     xchg    ah,al    ; byte swap
  396.     stosw        ; store in output
  397.     mov    ax,dx    ; retrieve low word and store in output
  398.     stosw
  399.     mov    ax,di    ; return incremented output pointer
  400.     pop    si
  401.     pop    di
  402.     pop    es
  403.     popds
  404.     pret
  405.     pend    put32
  406.  
  407. ; Convert 16-bit int to network order, returning new pointer
  408.     procdef    put16,<<cpo16,ptr>,<pi16,word>>
  409.     push    es
  410.     cld
  411.     push    di
  412.     ldptr    di,cpo16,es
  413.     mov    ax,pi16    ; fetch source word in machine order
  414.     xchg    ah,al    ; swap bytes
  415.     stosw        ; save in network order
  416.     mov    ax,di    ; return new output pointer to user
  417.     pop    di
  418.     pop    es
  419.     pret
  420.     pend    put16
  421.  
  422.     finish
  423.  
  424.     public    Tvec_
  425.  
  426. dataseg    segment    para public 'data'
  427. jtable    dw    l0,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13,l14,l15    
  428. dbase_    dw    seg _Dorg_
  429. cbase_    dw    seg _Corg_
  430.     bss    Spsave:word,2
  431.     bss    Sssave:word,2
  432.     bss    Intstk_:byte,512
  433.     bss    mtasker:byte,1
  434.     bss    Tvec_:word,4
  435. dataseg    ends
  436.     end
  437.